import 'package:demo/model/book_cell.dart';
import 'package:demo/model/book_nav.dart';
import 'package:demo/util/data_utils.dart';
import 'package:demo/widgets/book_list_cell.dart';
import 'package:flutter/material.dart';

class BookPageTabView extends StatefulWidget {
  final String categoryId;

  const BookPageTabView({Key key, this.categoryId}) : super(key: key);
  @override
  _BookPageTabViewState createState() => _BookPageTabViewState();
}

class _BookPageTabViewState extends State<BookPageTabView> {
  Map<String, dynamic> _params = {
    "category_id": "0",
    'cursor': '0',
    'limit': 20,
    'token': '',
  };
  List<BookCell> _bookList = <BookCell>[];

  Widget _itemBuilder(context, index) {
    return BookListCell(cellData: _bookList[index]);
  }

  getBookList() {
    if (widget.categoryId == '0') {
      _params['category_id'] = '0';
    } else {
      _params['category_id'] = widget.categoryId;
    }
    DataUtils.getBookListData(_params).then((resultList) {
      if (this.mounted) {
        setState(() {
          _bookList = resultList;
        });
      }
    });
  }

  @override
  void initState() {
    super.initState();
    getBookList();
  }

  @override
  Widget build(BuildContext context) {
    if (_bookList.length == 0) {
      return Center(
        child: CircularProgressIndicator(),
      );
    }
    return ListView.builder(
      itemBuilder: _itemBuilder,
      itemCount: _bookList.length,
    );
  }
}
